﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.MakeStaticPagingComponent.SystemServiceListing;
using BCDto.MakeStaticPagingComponent.WebsiteTemplate;
using BCService.MakeStaticPagingComponent.SystemServiceListingService;
using BCService.MakeStaticPagingComponent.WebsiteTemplateService;

namespace BCTMSRESTful.Controllers.Management.MakeStaticPagingComponent
{
    /// <summary>
    /// 系统服务列表信息控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "MakeStaticPage")]
    [Route("api/v1/sim/[controller]")]
    public class SystemServiceListingController : ManagementBaseController
    {
        private readonly IWebsiteTemplateService websiteTemplateService;
        private readonly ISystemServiceListingService systemServiceListingService;

        public SystemServiceListingController(IWebsiteTemplateService websiteTemplateService,
            ISystemServiceListingService systemServiceListingService)
        {
            this.websiteTemplateService = websiteTemplateService;
            this.systemServiceListingService = systemServiceListingService;
        }
        
        /// <summary>
        /// 获取系统服务列表信息返回对象的标记
        /// </summary>
        /// <param name="id">系统服务列表信息Id</param>
        /// <response code="200">返回系统服务列表信息返回对象的标记。</response>
        /// <returns></returns>
        [HttpGet("[action]/{id}")]
        [ProducesResponseType(typeof(IDictionary<string,string>), 200)]
        public IActionResult GetSystemServiceResultTags([FromRoute(Name = "id")]int id)
        {
            return this.Ok(this.systemServiceListingService.GetSystemServiceResultTags(id));
        } 

        /// <summary>
        /// 获取系统服务列表信息
        /// </summary>
        /// <param name="id">系统服务列表信息Id</param>
        /// <response code="200">返回系统服务列表信息。</response>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ProducesResponseType(typeof(SystemServiceListingDto), 200)]
        public IActionResult Get([FromRoute(Name = "id")]int id)
        {
            return this.Ok(this.systemServiceListingService.Get(id));
        }

        /// <summary>
        /// 获取系统服务列表信息列表
        /// </summary>
        /// <param name="websiteTemplateId">网站模板Id</param>
        /// <response code="200">返回系统服务列表信息列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<SystemServiceListingDto>), 200)]
        public IActionResult WebsiteService([FromQuery(Name ="website_template_id")]int? websiteTemplateId)
        {
            if (websiteTemplateId.HasValue)
            {
                return this.Ok(this.systemServiceListingService.Query(websiteTemplateId.Value));
            }
            else
            {
                WebsiteTemplateDto websiteTemplateDto = this.websiteTemplateService.GetUsed();
                return this.Ok(this.systemServiceListingService.Query(websiteTemplateDto.WebsiteTemplateId));
            }
            
        }
      
        /// <summary>
        /// 获取系统服务列表信息列表
        /// </summary>
        /// <param name="displayName">服务展示名称</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">返回系统服务列表信息列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<SystemServiceListingDto>), 200)]
        public IActionResult Get([FromQuery(Name = "display_name")]string displayName,
                [FromQuery(Name = "ascending")]bool? ascending,
                [FromQuery(Name = "index")]int index = 1,
                [FromQuery(Name = "size")]int size = 20)
        {
            WebsiteTemplateDto websiteTemplateDto = this.websiteTemplateService.GetUsed();
            return this.Ok(this.systemServiceListingService.Query(websiteTemplateDto.WebsiteTemplateId,displayName, ascending, index, size));
        }
      
        /// <summary>
        /// 添加系统服务列表信息
        /// </summary>
        /// <param name="systemServiceListingRequestDto">系统服务列表信息</param>
        /// <response code="201">返回系统服务列表信息。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SystemServiceListingDto), 201)]
        public IActionResult Post([FromBody]SystemServiceListingRequestDto systemServiceListingRequestDto)
        {
            SystemServiceListingDto result = this.systemServiceListingService.Add(systemServiceListingRequestDto);
            return this.Created(string.Format("api/v1/sim/SystemServiceListing/{0}", result.SystemServiceListingId), result);
        }

        /// <summary>
        /// 修改系统服务列表信息
        /// </summary>
        /// <param name="id">系统服务列表信息Id</param>
        /// <param name="systemServiceListingRequestDto">系统服务列表信息</param>
        /// <response code="200">返回系统服务列表信息。</response>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(SystemServiceListingDto), 200)]
        public IActionResult Put([FromRoute(Name = "id")]int id, [FromBody]SystemServiceListingRequestDto systemServiceListingRequestDto)
        {
            return this.Ok(this.systemServiceListingService.Update(id, systemServiceListingRequestDto));
        }

        /// <summary>
        /// 删除系统服务列表信息
        /// </summary>
        /// <param name="id">系统服务列表信息Id</param>
        /// <response code="204">没有返回信息。</response>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult Delete([FromRoute(Name = "id")]int id)
        {
            this.systemServiceListingService.Delete(id);
            return this.NoContent();
        }
    }
}
